package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.util.ConstantUtil;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ISearchService searchService;

    @Reference
    private TravelTemplateSearchService travelTemplateSearchService;

    @Reference
    private UserInfoTemplateSearchService userInfoTemplateSearchService;

    @Reference
    private DestinationTemplateSearchService destinationTemplateSearchService;

    @Reference
    private StrategyTemplateSearchService strategyTemplateSearchService;


    @RequestMapping("")
    public String index(Model model, HttpServletRequest request) {
        String token = CookieUtil.getToken(request, ConstantUtil.USER_INFO_TOKEN_COOKIE_NAME);
        if (token != null) {
            request.getSession().setAttribute(ConstantUtil.USER_INFO_IN_SESSION_NAME,
                    userInfoRedisService.getUserInfoByToken(token));
        }
        model.addAttribute("tcs", travelCommendService.selectHotTop5());
        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo") TravelQuery qo) {
        model.addAttribute("pageInfo", travelService.queryForList(qo));
        return "index/travelPageTpl";
    }

    @RequestMapping("/q")
    public Object search(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_TYPE_DESTINATION:
                return searchDestination(model, qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return searchStrategy(model, qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return searchTravel(model, qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return searchUser(model, qo);
            default:
                return searchAll(model, qo);
        }
    }

    private String searchAll(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.class, qo, "nickname", "destName");
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.class, qo, "title", "summary");
        Page<StrategyTemplate> strategies = searchService.searchWithHighlight(StrategyTemplate.class, qo, "title", "subTitle", "summary");
        Page<DestinationTemplate> destinations = searchService.searchWithHighlight(DestinationTemplate.class, qo, "name", "info");
        SearchResultVO resultVO = new SearchResultVO();
        resultVO.setTotal(users.getTotalElements() + travels.getTotalElements() + strategies.getTotalElements() + destinations.getTotalElements());
        resultVO.setDests(destinations.getContent());
        resultVO.setStrategys(strategies.getContent());
        resultVO.setUsers(users.getContent());
        resultVO.setTravels(travels.getContent());
        model.addAttribute("data", resultVO);
        return "index/searchAll";
    }

    private String searchUser(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page", page);
        return "index/searchUser";
    }

    private String searchTravel(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page", page);
        return "index/searchTravel";

    }

    private String searchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page", page);
        return "index/searchStrategy";
    }

    private String searchDestination(Model model, SearchQueryObject qo) {
        DestinationTemplate dest = destinationTemplateSearchService.queryByDestName(qo.getKeyword());
        if (dest != null) {
            List<UserInfoTemplate> users = userInfoTemplateSearchService.queryByDestName(qo.getKeyword());
            List<TravelTemplate> travels = travelTemplateSearchService.queryByDestName(qo.getKeyword());
            List<StrategyTemplate> strategies = strategyTemplateSearchService.queryByDestName(qo.getKeyword());
            SearchResultVO resultVO = new SearchResultVO();
            resultVO.setTotal(resultVO.getTotal() + users.size() + travels.size() + strategies.size());
            resultVO.setStrategys(strategies.size() > 5 ? strategies.subList(0, 5) : strategies);
            resultVO.setUsers(users.size() > 5 ? users.subList(0, 5) : users);
            resultVO.setTravels(travels.size() > 5 ? travels.subList(0, 5) : travels);
            model.addAttribute("data", resultVO);
        }
        model.addAttribute("dest", dest);
        return "index/searchDest";
    }

}
